#include "stdafx.h"
#include "wf_modifyrotate2.h"

#include "pf_snapper.h"



WF_ModifyRotate2::WF_ModifyRotate2(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Rotate Entities around two centers",
                           container, graphicView) {}


QAction* WF_ModifyRotate2::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Rotate Two")
	QAction* action = new QAction(tr("Rotate T&wo"), NULL);
	//action->zetStatusTip(tr("Rotate Entities around two centers"));
	action->setIcon(QIcon(":/extui/modifyrotate2.png"));
	return action;
}

void WF_ModifyRotate2::init(int status) {
    PF_ActionInterface::init(status);

}



void WF_ModifyRotate2::trigger() {

    PF_DEBUG->print("PF_ActionModifyRotate2::trigger()");

    PF_Modification m(*container, graphicView);
    m.rotate2(data);

    finish();

   	PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
}



void WF_ModifyRotate2::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionModifyRotate2::mouseMoveEvent begin");

    if (getStatus()==SetReferencePoint1 ||
            getStatus()==SetReferencePoint2) {

        PF_Vector mouse = snapPoint(e);
        switch (getStatus()) {
        case SetReferencePoint1:
            data.center1 = mouse;
            break;

        case SetReferencePoint2:
            if (data.center1.valid) {
                data.center2 = mouse;
                //data.offset = data.center2-data.center1;

                /*deletePreview();
                preview->addSelectionFrom(*container);
                preview->rotate(data.center1, data.angle);
                preview->move(data.offset);
                drawPreview();
                */
            }
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("PF_ActionModifyRotate2::mouseMoveEvent end");
}



void WF_ModifyRotate2::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_ModifyRotate2::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector pos = e->getCoordinate();

    switch (getStatus()) {
    case SetReferencePoint1:
        data.center1 = pos;
        setStatus(SetReferencePoint2);
        break;

    case SetReferencePoint2:
        data.center2 = pos;
        setStatus(ShowDialog);
        if (PF_DIALOGFACTORY->requestRotate2Dialog(data)) {
            trigger();
            //finish();
        }
        break;

    default:
        break;
    }
}


void WF_ModifyRotate2::commandEvent(PF_CommandEvent* /*e*/) {
}



PF_StringList WF_ModifyRotate2::getAvailableCommands() {
    PF_StringList cmd;
    return cmd;
}




void WF_ModifyRotate2::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetReferencePoint1:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify absolute reference point"),
                                            tr("Cancel"));
        break;
    case SetReferencePoint2:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify relative reference point"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ModifyRotate2::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyRotate2::updateToolBar() {
    switch (getStatus()) {
    case SetReferencePoint1:
    case SetReferencePoint2:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        break;
    default:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
        break;
    }
}


// EOF
